﻿using BTS.Common;
using BTS.Helpers;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using WPFLocalizeExtension.Engine;

namespace BTS
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

       public static Logger AppLog = new Logger(FilePaths.LogConfig, "AppLog", LogLevel.Debug);
        protected override void OnStartup(StartupEventArgs e)
        {
            EnsureSingleton();
            base.OnStartup(e);
            SystemConfig.Ins.LoadSystemConfig();
            InitializeCulture();
            AppLog = new Logger(FilePaths.LogConfig, "AppLog", SystemConfig.Ins.LogLevel);
            //  UpdateApp();
        }

        /// <summary>
        /// 初始化语言
        /// </summary>
        private void InitializeCulture()
        {
            // 切换语言
            CultureInfo cultureInfo = new CultureInfo(SystemConfig.Ins.Lang);
            LocalizeDictionary.Instance.Culture = cultureInfo;

        }
        private static Mutex AppMutex;
        private void EnsureSingleton()
        {
            AppMutex = new Mutex(true, "BTS", out var createdNew);

            if (createdNew)
            {
                return;
            }

            var current = Process.GetCurrentProcess();

            foreach (var process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    Win32Helper.SetForegroundWindow(process.MainWindowHandle);
                    break;
                }
            }

            Shutdown();
        }
    }

}
